package services;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

import models.Outlink;

public class UpdateOutlink {
	public UpdateOutlink(String domainName, List<String> domains) {
		try {
			ObjectifyService.register(Outlink.class);
		} catch (Exception e) {
			//e.printStackTrace();
		}
		Objectify ofy = ObjectifyService.begin();
		Outlink outlink = new Outlink();
		
		List<String> dms = new ArrayList<String>();
		for (int i = 0; i < domains.size(); i++) {
			if (i < 100) {
				dms.add(domains.get(i).replaceAll("(http|https)+://(www.)*", ""));
			}
		}
		
		outlink.setDomainName(domainName);
		outlink.setDomains(dms);
		outlink.setDate(new Date());
		
		ofy.put(outlink);
		System.out.println("Update outlink success: " + domainName);
	}
}
